<?php

//VoteDataCenter_migrateword_tambontemp();
/*
/////////////////////////////////////////////////
// This function use for migrate raw data to temp data
/////////////////////////////////////////////////
function VoteDataCenter_migrateword_temp(){
  //Initial value
  //$province_id = 96;

  /////////////////////////////////////////////////////////////////////
  //Migrate sex
  //Select sex from using data & update raw data to temp data
  ///////////////////////////////////////////////////////////////////
  $pro_id = FormUtil::getPassedValue ('pro_id', false , 'GET');
  if($pro_id){
    echo "Start migrate the sex table.<br>";
    //Selection
    $query = "SELECT
                sex.id,
                sex.name 
              FROM 
                suratrawdata.sex ";

    $column = array(
                      "id",
                      "name"
              );

    $result = DBUtil::executeSQL($query);
    $sexArray = DBUtil::marshallObjects ($result, $column);
    unset($result);

    foreach($sexArray as $item){
      //Update
      $where = "tempword.sex LIKE '%".$item["name"]."%'";
      $query = "UPDATE
                  suratrawdata.tempword
                SET
                  tempword.sex = ".$item["id"]."
                WHERE ".
                  $where;
        
      $result = DBUtil::executeSQL($query);
      unset($result);
    }
    unset($sexArray);
  }else{
    echo "You not set the sex id(sex_id)."; exit;
  }

  echo "<META HTTP-EQUIV=\"Refresh\" CONTENT=\"3;URL=http://".$_SERVER['SERVER_NAME'].$_SERVER['PHP_SELF']."?module=VoteDataCenter&type=migrate&func=provincetemp&pro_id=".$pro_id."\">";
  pnShutDown();
}

function VoteDataCenter_migrateword_provincetemp(){
  header ('Content-type: text/html; charset=utf-8');


  /////////////////////////////////////////////////////////////////////
  //Migrate province
  //Select province from using data & update raw data to temp data
  ///////////////////////////////////////////////////////////////////

  $pro_id = FormUtil::getPassedValue ('pro_id', false , 'GET');
  if($pro_id){
  echo "Start migrate the province table.<br>";
    //Selection
    $query = "SELECT
                province.id,
                province.name 
              FROM 
                suratrawdata.province
              WHERE 
                id = ".$pro_id;

    $column = array(
                      "id",
                      "name"
              );
    $result = DBUtil::executeSQL($query);
    $provinceArray = DBUtil::marshallObjects ($result, $column);
    unset($result);

    //Update
    $where = "tempword.province LIKE '%".$provinceArray[0]["name"]."%'";
    $query = "UPDATE
                suratrawdata.tempword
              SET
                tempword.province = ".$provinceArray[0]["id"]."
              WHERE ".
                $where;
      
    DBUtil::executeSQL($query);
    unset($provinceArray);
  }else{
    echo "You not set the province id(pro_id)."; exit;
  }

  echo "<META HTTP-EQUIV=\"Refresh\" CONTENT=\"3;URL=http://".$_SERVER['SERVER_NAME'].$_SERVER['PHP_SELF']."?module=VoteDataCenter&type=migrate&func=districttemp&pro_id=".$pro_id."\">";
  pnShutDown();
}

function VoteDataCenter_migrateword_districttemp(){
  header ('Content-type: text/html; charset=utf-8');
  /////////////////////////////////////////////////////////////////////
  //Migrate district
  //Select district from using data & update raw data to temp data
  ///////////////////////////////////////////////////////////////////
  $pro_id = FormUtil::getPassedValue ('pro_id', false , 'GET');
  if($pro_id){
    echo "Start migrate the district table.<br>";
    //Selection
    $query = "SELECT
                district.id,
                district.name 
              FROM 
                suratrawdata.district
              WHERE 
                district.pro_id = ".$pro_id;

    $column = array(
                      "id",
                      "name"
              );
    $result = DBUtil::executeSQL($query);
    $districtArray = DBUtil::marshallObjects ($result, $column);
    unset($result);

    $i=1;
    foreach($districtArray as $item){
      //Update
      $where = "tempword.distric = 'อำเภอ".$item["name"]."' OR  tempword.distric = '".$item["name"]."'";
      $query = "UPDATE
                  suratrawdata.tempword
                SET
                  tempword.distric = ".$item["id"]."
                WHERE ".
                  $where;

      $result = DBUtil::executeSQL($query);
      if($result){
        echo "District name : ".$item["name"]."<br>";
      }
    }
  }else{
    echo "You not set the district id."; exit;
  }

  echo "<META HTTP-EQUIV=\"Refresh\" CONTENT=\"3;URL=http://".$_SERVER['SERVER_NAME'].$_SERVER['PHP_SELF']."?module=VoteDataCenter&type=migrate&func=tambontemp&pro_id=".$pro_id."\">";
  pnShutDown();
}
*/

function VoteDataCenter_migrateword_tambontemp(){

  /////////////////////////////////////////////////////////////////////
  //Migrate district
  //Select district from using data & update raw data to temp data
  ///////////////////////////////////////////////////////////////////
  //Select rawdata
/*
  $pcolumn = array(
                    "pro_id"
                    );
  $pquery = "SELECT
              province
            FROM suratrawdata.tempword";

  $presult = DBUtil::executeSQL($pquery);
  $pobjectArray = DBUtil::marshallObjects ($presult, $pcolumn);
  unset($presult);
*/
    echo "Start migrate the tambon table.<br>";
    //Selection
    $query = "SELECT
                tambon.id,
                tambon.dis_id,
                tambon.name 
              FROM 
                suratrawdata.tambon
              WHERE 
                tambon.pro_id = '84'";

    $column = array(
                      "id",
                      "dis_id",
                      "name"
              );
    $result = DBUtil::executeSQL($query);
    $districtArray = DBUtil::marshallObjects ($result, $column);
    unset($result);

    //$i=1;
    foreach($districtArray as $item){
      //Update
      $where = "tempword.tumbol LIKE '%".$item["name"]."%' AND tempword.district = ".$item["dis_id"];
      $query = "UPDATE
                  suratrawdata.tempword
                SET
                  tempword.tumbol = ".$item["id"]."
                WHERE ".
                  $where;

      $result = DBUtil::executeSQL($query);
      if($result){
        echo "Tambon name : ".$item["name"]."<br>";
      }
    }
    echo "End migrate the tambon table.<br>";
  pnShutDown();


}



function VoteDataCenter_migrateword_copy(){
      echo "Start migrate the contact table.<br>";

  //Select rawdata
  $column = array(  
                    "sex",
                    "house_no",
                    "pid",
                    "first_name",
                    "last_name",
                    "no",
                    "unit",
                    "moo",
                    "tum_id",
                    "dis_id",
                    "pro_id"
                    );
  $query = "SELECT
                    sex,
                    house_no,
                    pid,
                    first_name,
                    last_name,
                    no,
                    unit,
                    moo,
                    tumbol,
                    distric,
                    province
            FROM klonghaerawdata.tempnamek";

  $result = DBUtil::executeSQL($query);
  $objectArray = DBUtil::marshallObjects ($result, $column);
  //var_dump($objectArray);
  //exit;
  unset($result);

  foreach($objectArray as $value){
    //insert rawdata to printout table
    $query = "INSERT INTO klonghaedemap.pn_degis_contact (
                                               aco_province_id,
                                               aco_district_id,
                                               aco_tumbol_id,
                                               aco_addr_moo,
                                               aco_unit,
                                               aco_no,
                                               aco_person_id,
                                               aco_gender_id,
                                               aco_first_name,
                                               aco_last_name,
                                               aco_addr_house_no
                                          )VALUES(
                                              '".$value['pro_id']."', 
                                              '".$value['dis_id']."', 
                                              '".$value['tum_id']."', 
                                              '".$value['moo']."', 
                                              '".$value['unit']."', 
                                              '".$value['no']."', 
                                              '".$value['pid']."', 
                                              '".$value['sex']."', 
                                              '".$value['first_name']."', 
                                              '".$value['last_name']."', 
                                              '".$value['house_no']."'
                                          )";

      DBUtil::executeSQL($query);
    }
    echo "End migrate the person table.<br>";
pnShutDown();


}






?>